package com.qy.controller.app;

import com.qy.base.core.*;
import com.qy.base.utils.DateUtil;
import com.qy.model.*;
import com.qy.service.*;
import com.github.pagehelper.PageHelper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.crypto.spec.OAEPParameterSpec;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zaq on 2018/07/10.
 */
@RestController
@RequestMapping("app/apply")
public class ApplyController {
    @Resource
    private ApplyService applyService;

    @Resource
    private MemberService memberService;

    @Resource
    private PolicyService policyService;

    @Resource
    private EvaluationService evaluationService;

    @Resource
    private MessageService messageService;

    @PostMapping("/add")
    public Result add(@RequestBody Apply apply) {
        applyService.save(apply);
        return ResultGenerator.successResult();
    }

    //删除
    @PostMapping("/delete")
    public Result delete(@RequestBody Apply apply) {
        if (apply.getId() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        applyService.deleteById(apply.getId());
        return ResultGenerator.successResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody Apply apply) {
        applyService.update(apply);
        return ResultGenerator.successResult();
    }

    @GetMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        Apply apply = applyService.findById(id);
        return ResultGenerator.successResult(apply);
    }

    @GetMapping("/list")
    public Result list(PageBean<Apply> page) {
        PageHelper.startPage(page.getPageNum(), page.getSize());
        List<Apply> list = applyService.findAll();
        page.setList(list);
        return ResultGenerator.successResult(page);
    }

    //申请政策
    @PostMapping("apply")
    public Result apply(@RequestBody Apply apply) {
        if (apply.getUser_id() == null || apply.getPolicy_id() == null || apply.getEvaluation_id() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        Member member = memberService.findById(apply.getUser_id());
        Policy policy = policyService.findById(apply.getPolicy_id());

        apply.setApplicant(member.getName());
        apply.setTelphone(member.getPhone());
        apply.setPolicy_name(policy.getPolicy_name());
        apply.setAdd_time(DateUtil.getNowTimestamp());
        applyService.save(apply);

        Message message = new Message();
        message.setMessage_type(3);
        message.setMessage_content(member.getNickname() + "提交了政策申请");
        message.setMessage_port(2);
        message.setAdd_time(DateUtil.getNowTimestamp());
        messageService.save(message);

        return ResultGenerator.successResult();
    }

    //申请记录
    @GetMapping("/applyList")
    public Result applyList(PageBean<Apply> page, Apply apply) {
        if (apply.getUser_id() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        PageHelper.startPage(page.getPageNum(), page.getSize());
        List<Apply> list = applyService.selectApplyListByUserId(apply);
        page.setList(list);
        return ResultGenerator.successResult(page);
    }

    //申请详情
    @GetMapping("/applyDetail")
    public Result applyDetail(Apply apply) {
        if (apply.getId() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        Apply apply1 = applyService.findById(apply.getId());
        Policy policy = policyService.findById(apply1.getPolicy_id());
        Evaluation evaluation = evaluationService.findById(apply1.getEvaluation_id());
        Map<String, Object> map = new HashMap<>();
        map.put("policy", policy);
        map.put("evaluation", evaluation);
        return ResultGenerator.successResult(map);
    }

}
